
const mongoose = require('mongoose');
const _ = require('lodash');
var orderService = require('../service/order.service');
// var customerService = require('../service/customer.service');
// var merchantService = require('../service/merchant.service');


module.exports={
    addOrder:addOrder,
    confirmOrder:confirmOrder,
    findByOrderId:findByOrderId,
    findOrders:findOrders
};


function addOrder(req,res,next){
    var OrderInfo= req.body;
    if(_.isEmpty(OrderInfo)){
        res.json({result:false,errMsg:'OrderInfo can not be empty!'});
    }else {
        orderService.addOrder(OrderInfo,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                res.json({result:true,data:data});
            }
        });
    }
}

function confirmOrder(req,res,next){
    var orderId = req.params.orderId;
    if(_.isEmpty(orderId)){
        res.json({result:false,errMsg:'orderId can not be empty!'});
    }else {
        orderService.confirmOrder(orderId,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                res.json({result:true,data:data});
            }
        });
    }
}


function findByOrderId(req,res,next){
    var orderId = req.params.orderId;
    if(_.isEmpty(orderId)){
        res.json({result:false,errMsg:'orderId can not be empty!'});
    }else {
        orderService.findByOrderId(orderId,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                res.json({result:true,data:data});
            }
        });
    }
}

function findOrders(req,res,next){
    var customerId = req.params.customerId;
    if(_.isEmpty(customerId)){
        res.json({result:false,errMsg:'customerId can not be empty!'});
    }else {
        orderService.findOrders(customerId,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                res.json({result:true,data:data});
            }
        });
    }
}


// function findOrderByCustomerId(req,res,next){
//     var customerId = req.params.customerId;
//     if(_.isEmpty(customerId))
// }

